package lumis.portal.presentation.perspective.admin;

import lumis.portal.presentation.tag.page.RenderHolderComponent;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;

/**
 * Behavior that provides the javascript for removing an interface,
 * during the edit of a page.
 *
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
public class RemoveInterfaceBehavior extends AbstractDefaultAjaxBehavior
{
	@Override
	protected void respond(final AjaxRequestTarget target)
	{
		// get serviceInterfaceInstanceId
		MarkupContainer container = (MarkupContainer) getComponent();
		final String serviceInterfaceInstanceId = container.getRequest().getParameter("ii");

		// remove serviceInterfaceInstanceId from a holder 
		container.visitChildren(RenderHolderComponent.class, new IVisitor<RenderHolderComponent>()
		{
			public Object component(RenderHolderComponent component)
			{
				if (component.removeServiceInterfaceInstance(serviceInterfaceInstanceId))
				{
					target.addComponent(component);
					return STOP_TRAVERSAL;
				}
				else
				{
					return CONTINUE_TRAVERSAL;
				}
			}
		});
	}
	
	@Override
	public void renderHead(IHeaderResponse response)
	{
		response.renderJavascript("function removeInterface(ii)" +
				"{" +
					"wicketAjaxPost('lumis/portal/presentation/layoutfile/" + getCallbackUrl() + 
					"','ii='+ii,function(){g_LumisPage.onHolderRefresh()}.bind(this),function(){}.bind(this),function(){return true;}.bind(this));" +
				"}", "removeInterfaceFunction");
	}
}